package net.zjitc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Order;
import net.zjitc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
public class OrderContrller {
    @Autowired
    private OrderService orderService;

    /**
     * 订单数据列表
     * @param query
     * @param pagenum
     * @param pagesize
     * @return
     */
    @GetMapping("/orders")
    public ResponseResult findAll(@RequestParam(required = false) Integer query,
                                  @RequestParam(value = "pagenum",required = true) Integer pagenum,
                                  @RequestParam(value = "pagesize",required = true) Integer pagesize){
        Page<Order> page=orderService.selectPage(pagenum,pagesize);
        ResponseResult result=new ResponseResult();
        result.setSuccess("获取成功",page);
        return result;
    }
    /**
     * 修改订单状态
     * @param id
     * @param order
     * @return
     */
    @PutMapping("/orders/{id}")
    public ResponseResult update(@PathVariable Integer id,
                                 @RequestBody Order order){
        Order order1=orderService.update(id,order);
        ResponseResult result=new ResponseResult();
        result.setSuccess("获取成功",order1);
        return result;
    }
    @GetMapping("orders/{id}")
    public ResponseResult findById(@PathVariable Integer id){
        ResponseResult result=new ResponseResult();
        Order order=orderService.findById(id);
        if (order!=null){
            result.setSuccess("获取成功",order);
        }else {
            result.setError("获取失败");
        }
        return result;
    }
}
